'use strict';

const Controller = require('egg').Controller;

class EmployeeController extends Controller {
  // 获取企业员工列表
  async getEnterpriseEmployees() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    const { 
      page = 1, 
      pageSize = 10, 
      keyword = '', 
      phone = '',
      status = '', 
      hireTimeStart = '',
      hireTimeEnd = ''
    } = ctx.query;
    
    try {
      const result = await ctx.service.job1.employee.getEnterpriseEmployees(enterpriseId, { 
        page, 
        pageSize, 
        keyword, 
        phone,
        status,
        hireTimeStart,
        hireTimeEnd
      });
      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '获取员工列表失败'
      };
    }
  }

  // 创建员工
  async createEmployee() {
    const { ctx } = this;
    const employeeData = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.employee.createEmployee(employeeData);
      ctx.body = {
        code: 0,
        data: result,
        msg: '员工创建成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '创建员工失败'
      };
    }
  }

  // 更新员工
  async updateEmployee() {
    const { ctx } = this;
    const { employeeId } = ctx.params;
    const updateData = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.employee.updateEmployee(employeeId, updateData);
      ctx.body = {
        code: 0,
        data: result,
        msg: '员工更新成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '更新员工失败'
      };
    }
  }

  // 删除员工
  async deleteEmployee() {
    const { ctx } = this;
    const { employeeId } = ctx.params;
    
    try {
      await ctx.service.job1.employee.deleteEmployee(employeeId);
      ctx.body = {
        code: 0,
        data: null,
        msg: '员工删除成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '删除员工失败'
      };
    }
  }

  // 批量操作员工
  async batchOperateEmployees() {
    const { ctx } = this;
    const { employeeIds, operation } = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.employee.batchOperateEmployees(employeeIds, operation);
      ctx.body = {
        code: 0,
        data: result,
        msg: '批量操作成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '批量操作失败'
      };
    }
  }

  // 获取所有员工（管理员）
  async getAllEmployees() {
    const { ctx } = this;
    const { page = 1, pageSize = 10, name = '' } = ctx.query;

    try {
      const where = {};
      if (name) {
        where.employeeName = { [ctx.app.Sequelize.Op.like]: `%${name}%` };
      }

      const { count, rows } = await ctx.model.Employee.findAndCountAll({
        where,
        limit: parseInt(pageSize),
        offset: (parseInt(page) - 1) * parseInt(pageSize),
        order: [['hireTime', 'DESC']],
        include: [{
          model: ctx.model.Enterprise,
          as: 'enterprise',
          attributes: ['enterpriseId', 'enterpriseName']
        }]
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: rows,
        pagination: {
          current: parseInt(page),
          pageSize: parseInt(pageSize),
          total: count,
          pages: Math.ceil(count / parseInt(pageSize))
        }
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: '获取员工列表失败: ' + error.message,
        data: null
      };
    }
  }
}

module.exports = EmployeeController;














